// 云函数测试文件
export default class CloudFunctionTest {
  constructor() {
    this.openid = null
  }

  // 测试获取OpenID
  async testGetOpenId() {
    try {
      console.log('测试获取OpenID...')
      const { result } = await wx.cloud.callFunction({
        name: 'getOpenId'
      })
      console.log('OpenID测试结果:', result)
      this.openid = result.openid
      return result
    } catch (error) {
      console.error('获取OpenID失败:', error)
      throw error
    }
  }

  // 测试房间管理云函数
  async testRoomManager(type, data = {}) {
    try {
      console.log(`测试房间管理云函数，类型: ${type}，数据:`, data)
      const { result } = await wx.cloud.callFunction({
        name: 'roomManager',
        data: {
          type: type,
          openid: this.openid,
          ...data
        }
      })
      console.log('房间管理测试结果:', result)
      return result
    } catch (error) {
      console.error('房间管理云函数测试失败:', error)
      throw error
    }
  }

  // 运行完整测试
  async runFullTest() {
    try {
      console.log('开始云函数测试...')
      
      // 1. 测试获取OpenID
      await this.testGetOpenId()
      
      // 2. 测试创建房间
      const roomId = 'test_room_' + Date.now()
      const createResult = await this.testRoomManager('create_room', {
        roomId: roomId,
        playerName: '测试玩家1'
      })
      
      if (createResult.success) {
        console.log('创建房间测试成功')
        
        // 3. 测试获取房间信息
        const infoResult = await this.testRoomManager('get_room_info', {
          roomId: roomId
        })
        
        if (infoResult.success) {
          console.log('获取房间信息测试成功')
        }
        
        // 4. 测试离开房间
        const leaveResult = await this.testRoomManager('leave_room', {
          roomId: roomId,
          playerName: '测试玩家1'
        })
        
        if (leaveResult.success) {
          console.log('离开房间测试成功')
        }
      }
      
      console.log('所有测试完成')
    } catch (error) {
      console.error('测试失败:', error)
      throw error
    }
  }
}
